package com.chenyang.mediaplay.fragment;

import java.util.ArrayList;

import com.chenyang.mediaplay.R;
import com.chenyang.mediaplay.activity.AudioPlayerActivity;
import com.chenyang.mediaplay.adapter.AudioListAdapter;
import com.chenyang.mediaplay.bean.AudioItem;
import com.chenyang.mediaplay.interfaces.Keys;

import android.content.AsyncQueryHandler;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore.Audio.Media;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MusicFragment extends BaseFragment {

	private ListView listView;

	@Override
	public void initListener() {
		// TODO Auto-generated method stub
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				//获取与列表中指定位置相关联的数据。
				Cursor cursor = (Cursor) parent.getItemAtPosition(position);
				
				ArrayList<AudioItem> audios = getAudios(cursor);
				startAudioPlayerActivity(audios, position);
				
				
			}
			
		});
	}
	
	protected void startAudioPlayerActivity(ArrayList<AudioItem> audios,
			int position) {
		// TODO Auto-generated method stub
		Intent intent = new Intent(getActivity(),AudioPlayerActivity.class);
		intent.putExtra(Keys.ITEMS, audios);
		intent.putExtra(Keys.CURRENT_POSITION, position);
		startActivity(intent);
	}

	/**
	 * 把Cursor里面的数据取出来封装到集合当中
	 * @param cursor
	 * @return
	 */
	
	protected ArrayList<AudioItem> getAudios(Cursor cursor) {
		// TODO Auto-generated method stub
		ArrayList<AudioItem> audios = new ArrayList<AudioItem>();
		cursor.moveToFirst();
		do {
			audios.add(AudioItem.fromCursor(cursor));
		} while (cursor.moveToNext());
		
		return audios;
	}

	@Override
	public void initData() {
		AsyncQueryHandler queryHandler = new AsyncQueryHandler(getActivity().getContentResolver()) {
			@Override
			protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
				// Utils.printCursor(cursor);
				AudioListAdapter adapter = new AudioListAdapter(getActivity(), cursor);
				listView.setAdapter(adapter);
			}
		};
		
		int token = 0;			// 相当于Message.what
		Object cookie = null;	// 相当于Message.obj
		Uri uri = Media.EXTERNAL_CONTENT_URI;
		String[] projection = {			// 指定要查询哪些列
				Media._ID, Media.TITLE, Media.ARTIST, Media.DATA
		};
		String selection = null;		// 指定要查询的条件
		String[] selectionArgs = null;	// 指定查询条件中的参数
		String orderBy = Media.TITLE + " ASC"; 	// 指定为升序
		
		//查询操作运行在子线程中
		queryHandler.startQuery(token, cookie, uri, projection, selection, selectionArgs, orderBy);
		
	}

	@Override
	public void initView() {
		
		listView = (ListView) rootView;
		
	}

	@Override
	public int getLayoutResID() {
		// TODO Auto-generated method stub
		return R.layout.fragment_music;
	}

	@Override
	public void onClick(View v, int id) {
		// TODO Auto-generated method stub

	}

}
